﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml.Linq;
using 立库托盘入库.bean;
using 立库托盘入库.Until;
using 立库托盘入库.ViewModel;

namespace 立库托盘入库
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        MainViewModel _viewModel;
        private string path;

        public  MainWindow() 
        {
            InitializeComponent();
            path = AppDomain.CurrentDomain.BaseDirectory;
            _viewModel = MainViewModel.MainViewModelCreate();
            base.DataContext = _viewModel;
            //this.WindowState = System.Windows.WindowState.Normal;
            //this.WindowStyle = System.Windows.WindowStyle.None;
            //this.ResizeMode = System.Windows.ResizeMode.NoResize;
            //this.Topmost = true;

            //this.Left = 0.0;
            //this.Top = 0.0;
            //this.Width = System.Windows.SystemParameters.PrimaryScreenWidth;
            //this.Height = System.Windows.SystemParameters.PrimaryScreenHeight;
            _viewModel.initdata();
            this.WindowState = System.Windows.WindowState.Maximized; 
            _viewModel.xunhuan();
           
            _viewModel.getData();
            
            _viewModel.MyCom = new ComUntil(_viewModel);
            _viewModel.sxcom();
            readXUserName();
        }
        //读取设置的叉车操作员工
        public void readXUserName()
        {
            try
            {
                if (Directory.Exists(path + "save") == false)//如果不存在就创建file文件夹
                {
                    Directory.CreateDirectory(path + "save");
                }
                if (File.Exists(path + "save\\XUserName.xml"))
                {
                    string filePath = path + "save\\XUserName.xml";
                    XElement xmlDoc = XElement.Load(filePath);
                    //XElement d = xmlDoc.Element("UserData");
                    var data = xmlDoc.Descendants("data");
                    foreach (var item in data)
                    {
                        UserBean bean = new UserBean();

                        bean.UserName = item.Element("UserName").Value;
                        bean.Uid = item.Element("Uid").Value;
                        _viewModel.XUserBeans.Add(bean);
                    }
                    List<string> ktls = new List<String>(); ;
                    foreach (UserBean bean in _viewModel.XUserBeans)
                    {
                        ktls.Add(bean.UserName);
                    }

                    _viewModel.UserNames = ktls.ToArray();
                }
                else
                {
                    //不存在文件
                    //Directory.CreateDirectory(path + "save\\Login.xml");//创建该文件

                    string filePathOne = System.IO.Path.Combine(path + "save", "XUserName.xml");
                    File.Create(filePathOne);
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine("sssssssssssss" + e.ToString());

            }
            
        }

        private void Onclick_quxiaowancheng(object sender, MouseButtonEventArgs e)
        {
            var btn = sender as Label;
            var c = btn.DataContext as panItemBean;
            if (c.BufferFinishStatu == "是")
            {
                _viewModel.quxiaoshoudongRuku(c.PalletNum);
            }
            
        }

        private void Onclick_ChakanOnum(object sender, MouseButtonEventArgs e)
        {
            var btn = sender as Label;
            var c = btn.DataContext as panItemBean;
            Process.Start("explorer.exe", "https://erp.knoya.com/scdd/" + c.Onum);
        }

        private void OnClick_Clean(object sender, RoutedEventArgs e)
        {
            _viewModel.getData();
        }

        private void OnButtonClick_Quxiao(object sender, RoutedEventArgs e)
        {
            var btn = sender as Button;
            var c = btn.DataContext as panItemBean;
            _viewModel.deletePallte(c.PalletNum);
        }

        private void OnClick_ChacheUser(object sender, RoutedEventArgs e)
        {
            Debug.WriteLine("sssssssssssss");
            _viewModel.ChacheUsernameContent = new ChacheUsernameDialog();
            _viewModel.isChacheUsername = Visibility.Visible;
        }

        private void OnClick_Setting_Saomiao(object sender, RoutedEventArgs e)
        {
            Debug.WriteLine("sssssssssssss");
            _viewModel.SettinfScanContent = new SettinfScan();
            _viewModel.IsScanDialogOpen = Visibility.Visible;
        }
        

    }
}
